package com.deyuanyun.pic.settings.controller.base;


import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.settings.enumeration.ThresholdType;
import com.deyuanyun.pic.settings.mapper.base.BizThresholdMapper;
import com.deyuanyun.pic.settings.service.base.BizMsgThresholdService;
import com.deyuanyun.pic.settings.service.base.BizThresholdTaskService;
import com.deyuanyun.pic.settings.task.util.QuartzManagerUtil;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

/**
 * 任务调度管理controller，暂为测试提供
 *
 * @author axiao(xj08.dream@gmail.com)
 * @version 1.0
 *          Histroy: [修改人][时间][描述]
 * @date 2016-08-04
 */
@Controller
@RequestMapping("/quartz")
public class QuartzManagerController {

    @Autowired
    private BizThresholdTaskService bizThresholdTaskService;
    @Autowired
    private BizMsgThresholdService bizMsgThresholdService;

    @Autowired
    private BizThresholdMapper bizThresholdMapper;

    @RequestMapping("/toQuartz.html")
    public String toQuartz(ModelMap modelMap) {
        TimeZone timeZone = TimeZone.getDefault();
        modelMap.addAttribute("zone", timeZone.getID());
        return "/base/quartzManager";
    }

    /**
     * 得到所有的任务
     */
    @RequestMapping("/getAll.json")
    @ResponseBody
    public void getAllJob() {
        AjaxSupport.sendSuccessText("success", StringUtils.DATE_TIME_DEFAULT, QuartzManagerUtil.getAllJob());
    }

    /**
     * 删除指定任务名任务组的任务
     *
     * @param name  任务名
     * @param group 任务组名
     */
    @RequestMapping("/deleteJob.json")
    @ResponseBody
    public void deleteJob(String name, String group) {
        if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(group)) {
            JobKey jobKey = JobKey.jobKey(name, group);
            QuartzManagerUtil.removeJob(jobKey);
            AjaxSupport.sendSuccessText("success", "删除任务名为:" + name + "，组名为：" + group + "任务成功");
        } else {
            AjaxSupport.sendFailText("error", "删除任务名为:" + name + "，组名为：" + group + "任务失败");
        }
    }

    /**
     * 修改cron表达式
     * @param name 任务名
     * @param group 组名
     * @param cron 表达式
     */
    @RequestMapping("/modifyJobTime.json")
    @ResponseBody
    public void modifyJobTime(String name, String group, String cron) {
        if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(group) && StringUtils.isNotEmpty(cron)) {
            QuartzManagerUtil.modifyJobTime(name, group, cron);
            AjaxSupport.sendSuccessText("success", "修改任务名为:" + name + "，组名为：" + group + "的cron成功");
        } else {
            AjaxSupport.sendFailText("error", "修改任务名为:" + name + "，组名为：" + group + "的cron失败");
        }
    }

    /**
     * 立即运行任务
     * @param name 任务名
     * @param group 组名
     */
    @RequestMapping("/startJobNow.json")
    @ResponseBody
    public void startJobNow(String name, String group) {
        if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(group)) {
            QuartzManagerUtil.startJobNow(name, group);
            AjaxSupport.sendSuccessText("success", "立即运行任务名为:" + name + "，组名为：" + group + "的成功");
        } else {
            AjaxSupport.sendFailText("error", "立即运行任务名为:" + name + "，组名为：" + group + "的失败");
        }
    }


    /**
     * 暂停任务
     * @param name 任务名
     * @param group 组名
     */
    @RequestMapping("/pauseJob.json")
    @ResponseBody
    public void pauseJob(String name, String group) {
        if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(group)) {
            QuartzManagerUtil.pauseJob(name, group);
            AjaxSupport.sendSuccessText("success", "暂停任务名为:" + name + "，组名为：" + group + "的成功");
        } else {
            AjaxSupport.sendFailText("error", "暂停任务名为:" + name + "，组名为：" + group + "的失败");
        }
    }

    /**
     * 恢复任务
     * @param name 任务名
     * @param group 组名
     */
    @RequestMapping("/resumeJob.json")
    @ResponseBody
    public void resumeJob(String name, String group) {
        if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(group)) {
            QuartzManagerUtil.resumeJob(name, group);
            AjaxSupport.sendSuccessText("success", "恢复任务名为:" + name + "，组名为：" + group + "的成功");
        } else {
            AjaxSupport.sendFailText("error", "恢复任务名为:" + name + "，组名为：" + group + "的失败");
        }
    }

    /**
     * 删除所有任务
     */
    @RequestMapping("/deleteAllJob.json")
    @ResponseBody
    public void deleteAllJob() throws Exception {
        Scheduler scheduler = QuartzManagerUtil.getScheduler();
        GroupMatcher groupMatcher = GroupMatcher.anyGroup();
        Set<JobKey> jobKeySet = scheduler.getJobKeys(groupMatcher);
        List<JobKey> jobKeys = new ArrayList<JobKey>(jobKeySet);
        QuartzManagerUtil.removeJob(jobKeys);
        AjaxSupport.sendSuccessText("success", "删除所有任务成功");
    }

    /**
     * 初始化所有推送设置的任务
     */
    @RequestMapping("/initTask.json")
    @ResponseBody
    public void initTask() {
        bizThresholdTaskService.checkClientFailData();//检测客户端调用接口失败任务调度
        bizThresholdTaskService.riskSystemCycle();//风险评价风险等级任务调度
        bizThresholdTaskService.pipeCycle(ThresholdType.RISK_PIPE_SCHEDULE.getValue());//风险评价管道周期提醒任务调度
        bizThresholdTaskService.pipeCycle(ThresholdType.INTERNAL_INSP_PIPE_SCHEDULE.getValue());//完整性评价内检测管道周期提醒任务调度
        bizThresholdTaskService.pipeCycle(ThresholdType.INTERNAL_EXTE_PIPE_SCHEDULE.getValue());//完整性评价外检测管道周期提醒任务调度
        AjaxSupport.sendSuccessText("success", "推送设置默认任务初始化成功");
    }

    /**
     * 删除推送设置默认的任务
     */
    @RequestMapping("/deleteDefaultTask.json")
    @ResponseBody
    public void deleteDefaultTask() {
        bizThresholdTaskService.deleteDefaultTask();
        AjaxSupport.sendSuccessText("success", "删除推送设置默认任务成功");
    }

}
